package main

func findAnagrams(s string, p string) []int {
	if len(s) < len(p) {
		return []int{}
	}
	digest := make(map[byte]int)
	subDigest := make(map[byte]int)
	for i, b := range p {
		digest[byte(b)]++
		subDigest[byte(s[i])]++
	}

	var result []int
	if isMatch(digest, subDigest) {
		result = append(result, 0)
	}
	for i := range s {
		if i+len(p) >= len(s) {
			break
		}
		subDigest[s[i]]--
		subDigest[s[i+len(p)]]++
		if isMatch(subDigest, digest) {
			result = append(result, i+1)
		}
	}
	return result
}

func isMatch(subDigest, digest map[byte]int) bool {
	for k, v := range digest {
		if v != subDigest[k] {
			return false
		}
	}
	return true
}

func main() {
	ret := findAnagrams("cbaebabacd", "abc")
	println(ret)
}
